package com.weixin.weixinmanage.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;
import com.weixin.weixinmanage.entity.SubTag;
import com.weixin.weixinmanage.entity.Tag;
import com.weixin.weixinmanage.service.SubTagService;
import com.weixin.weixinmanage.service.TagService;

public class TagAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5689163701843641046L;
	
	private Logger log = Logger.getLogger(TagAction.class);
	
	private Tag tag;
	
	private SubTag subTag;
	
	private TagService tagService;
	
	private SubTagService subTagService;
	
	private HashMap<String, Object> queryJson;
	
	
	/**
	 * 添加标签
	 * @return
	 */
	public String add(){
		queryJson = new HashMap<String, Object>();
		try{
			tagService.add(tag);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		tag.setTagId(null);
		return "add";
	}
	
	/**
	 * 删除标签
	 * @return
	 */
	public String del(){
		queryJson = new HashMap<String, Object>();
		try{
			tagService.update(tag);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		tag.setTagId(null);
		return "del";
	}
	
	/**
	 * 添加子标签
	 * @return
	 */
	public String addSub(){
		queryJson = new HashMap<String, Object>();
		try{
			subTagService.add(subTag);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		return "addSub";
	}
	
	/**
	 * 首页加载所有数据
	 * @return
	 */
	public String getAllData(){
		queryJson = new HashMap<String, Object>();
		List<Tag> tagList = new ArrayList<Tag>();
		List<List<SubTag>> subTagList = new ArrayList<List<SubTag>>();
		try{
			tagList = tagService.select(tag);
			for(Tag tagg:tagList){
				subTag.setTagId(tagg.getTagId());
				List<SubTag> list = subTagService.select(subTag);
				subTagList.add(list);
			}
		}catch(Exception e){
			e.printStackTrace();
			tagList = new ArrayList<Tag>();
			subTagList = new ArrayList<List<SubTag>>();
		}
		queryJson.put("tagList", tagList);
		queryJson.put("subTagList", subTagList);
		return "getAllData";
	}
	
	/**
	 * 获取子标签列表
	 * @return
	 */
	public String getSubData(){
		queryJson = new HashMap<String, Object>();
		List<SubTag> subTagList = new ArrayList<SubTag>();
		try{
			subTagList = subTagService.select(subTag);
		}catch(Exception e){
			subTagList = new ArrayList<SubTag>();
		}
		queryJson.put("subTagList", subTagList);
		return "getSubData";
	}
	
	/**
	 * 修改一个子标签跳页面
	 * @return
	 */
	public String editOneSub(){
		try {
			subTag = subTagService.selectById(subTag.getSubTagId());
		} catch (Exception e) {
			log.error("", e);
		}
		return "editOneSub";
	}
	
	
	/**
	 * 修改一个子标签
	 * @return
	 */
	public String toEditOneSub(){
		queryJson = new HashMap<String, Object>();
		try{
			subTagService.update(subTag);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		return "toEditOneSub";
	}
	

	public Tag getTag() {
		return tag;
	}

	public void setTag(Tag tag) {
		this.tag = tag;
	}


	public TagService getTagService() {
		return tagService;
	}


	public void setTagService(TagService tagService) {
		this.tagService = tagService;
	}


	public HashMap<String, Object> getQueryJson() {
		return queryJson;
	}


	public void setQueryJson(HashMap<String, Object> queryJson) {
		this.queryJson = queryJson;
	}


	public SubTagService getSubTagService() {
		return subTagService;
	}


	public void setSubTagService(SubTagService subTagService) {
		this.subTagService = subTagService;
	}
	
	
	public SubTag getSubTag() {
		return subTag;
	}

	public void setSubTag(SubTag subTag) {
		this.subTag = subTag;
	}
}
